package com.insigma.business.qrcode.soldier.util;


import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

public class AESUtil {
    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
    private static final String CIPHER_ALGORITHM_1 = "AES/CBC/NoPadding";
    private static final String CIPHER_ALGORITHM_2 = "AES/CBC/PKCS5Padding";
    private static final String CIPHER_ALGORITHM_3 = "AES/CBC/ISO10126Padding";
    private static final String CIPHER_ALGORITHM_4 = "AES/CFB/NoPadding";
    private static final String CIPHER_ALGORITHM_5 = "AES/CFB/PKCS5Padding";
    private static final String CIPHER_ALGORITHM_6 = "AES/CFB/ISO10126Padding";
    private static final String CIPHER_ALGORITHM_7 = "AES/ECB/NoPadding";
    private static final String CIPHER_ALGORITHM_8 = "AES/ECB/ISO10126Padding";
    private static final String CIPHER_ALGORITHM_9 = "AES/OFB/NoPadding";
    private static final String CIPHER_ALGORITHM_10 = "AES/OFB/PKCS5Padding";
    private static final String CIPHER_ALGORITHM_11 = "AES/OFB/ISO10126Padding";
    private static final String CIPHER_ALGORITHM_12 = "AES/PCBC/NoPadding";
    private static final String CIPHER_ALGORITHM_13 = "AES/PCBC/PKCS5Padding";
    private static final String CIPHER_ALGORITHM_14 = "AES/PCBC/ISO10126Padding";

    private AESUtil() {
    }

    public static byte[] initSecretKey(int length) {
        KeyGenerator kg = null;

        try {
            kg = KeyGenerator.getInstance("AES");
        } catch (NoSuchAlgorithmException var3) {
            return new byte[0];
        }

        kg.init(length);
        SecretKey secretKey = kg.generateKey();
        return secretKey.getEncoded();
    }

    public static Key toKey(byte[] key) {
        return new SecretKeySpec(key, "AES");
    }

    public static Key initKey(int length) throws NoSuchAlgorithmException {
        KeyGenerator kg = null;
        kg = KeyGenerator.getInstance("AES");
        kg.init(length);
        SecretKey secretKey = kg.generateKey();
        return new SecretKeySpec(secretKey.getEncoded(), "AES");
    }

    public static byte[] encrypt(byte[] data, Key key) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
        return encrypt(data, key, "AES/ECB/PKCS5Padding");
    }

    public static byte[] encrypt(String data, Key key) throws InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException {
        byte[] datas = data.getBytes();
        return encrypt(datas, key);
    }

    public static byte[] encrypt(byte[] data, byte[] key) throws InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException {
        return encrypt(data, key, "AES/ECB/PKCS5Padding");
    }

    public static byte[] encrypt(String data, byte[] key) throws InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException {
        return encrypt(data.getBytes(), key, "AES/ECB/PKCS5Padding");
    }

    public static byte[] encrypt(byte[] data, byte[] key, String cipherAlgorithm) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
        Key k = toKey(key);
        return encrypt(data, k, cipherAlgorithm);
    }

    public static byte[] encrypt(String data, byte[] key, String cipherAlgorithm) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
        Key k = toKey(key);
        return encrypt(data.getBytes(), k, cipherAlgorithm);
    }

    public static byte[] encrypt(byte[] data, Key key, String cipherAlgorithm) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Cipher cipher = Cipher.getInstance(cipherAlgorithm);
        cipher.init(1, key);
        return cipher.doFinal(data);
    }

    public static byte[] encrypt(String data, Key key, String cipherAlgorithm) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Cipher cipher = Cipher.getInstance(cipherAlgorithm);
        cipher.init(1, key);
        return cipher.doFinal(data.getBytes());
    }

    public static byte[] decrypt(byte[] data, byte[] key) throws InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException {
        return decrypt(data, key, "AES/ECB/PKCS5Padding");
    }

    public static byte[] decrypt(byte[] data, Key key) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
        return decrypt(data, key, "AES/ECB/PKCS5Padding");
    }

    public static byte[] decrypt(byte[] data, byte[] key, String cipherAlgorithm) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
        Key k = toKey(key);
        return decrypt(data, k, cipherAlgorithm);
    }

    public static byte[] decrypt(byte[] data, Key key, String cipherAlgorithm) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Cipher cipher = Cipher.getInstance(cipherAlgorithm);
        cipher.init(2, key);
        return cipher.doFinal(data);
    }
}
